package com.smarthospital.service.impl;

import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.contants.SHConst;
import com.smarthospital.dao.daoService.InquiryOrderDaoService;
import com.smarthospital.entity.pojo.InquiryOrder;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.RTCService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * create by sym  2021/5/22 12:48.
 */
@Slf4j
@Service
public class RTCServiceImpl implements RTCService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private InquiryOrderDaoService inquiryOrderDaoService;
    /**
     * 音视频通话 通知
     * 在开始的时候将时间戳存在redis里面 结束的时候取出来 计算时间
     * @param orderId
     * @param type
     * @param piont
     * @return 剩余通话时长
     */
    @Override
    public int videoAdvice(String orderId, Integer type, Integer piont) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = inquiryOrderDaoService.getOrderById(Long.valueOf(orderId));
        if (order == null) {
            throw new CommonException(CodeMsg.ERROR_NO_ORDER);
        }
        if (!user.getId().equals(order.getDoctorId())&&!user.getId().equals(order.getInquiryManId())) {
            throw new CommonException(CodeMsg.ERROR_NO_permission);
        }
        String timeStamp = System.currentTimeMillis() + "";

        log.info("videoAdvice_ 用户【{}】{}{}通话 TimeStamp {}",user.getRegisterId(),piont==0?"发起":"结束了",type==0?"视频":"音频",timeStamp);
        String key = String.format(SHConst.redis_key.videoAdvice_time,orderId);

        if (0 == piont) { //开始
            //判断订单是否有剩余时间
            Integer leftVedioTime = order.getLeftVedioTime();
            if (0 >= leftVedioTime) {
                throw new CommonException(CodeMsg.ERROR_NO_LEFT_TIME);
            }
            redisTemplate.opsForValue().set(key,timeStamp,1L,TimeUnit.DAYS);
            return leftVedioTime;

        }else if (1 == piont){//结束
            String cacheTimeStamp = redisTemplate.opsForValue().get(key);
            if (StringUtils.isEmpty(cacheTimeStamp)) {
                throw new CommonException(CodeMsg.ERROR_NO_VEDIO);
            }

            //本次通话耗时 分钟
            long time = (System.currentTimeMillis() - Long.valueOf(cacheTimeStamp)) / (1000 * 60);
            log.info("videoAdvice_ 用户【{}】{}通话{}分钟",user.getRegisterId(),type==0?"视频":"音频",time);
            Integer leftVedioTime = order.getLeftVedioTime()-Integer.valueOf(time+"") ;
            //小于0 取0
            leftVedioTime =leftVedioTime<0?0:leftVedioTime;
            order.setLeftVedioTime(leftVedioTime);
            inquiryOrderDaoService.update(order);
            return leftVedioTime;
        }else {
            throw new CommonException(CodeMsg.ERROR_PARAM_POINT);
        }
    }

    /**
     * 检验用户能否发起通话
     * @param orderId
     */
    @Override
    public void check(String orderId) {
        User user = LoginInterceptor.getUser();
        InquiryOrder order = inquiryOrderDaoService.getOrderById(Long.valueOf(orderId));
        if (order == null) {
            throw new CommonException(CodeMsg.ERROR_NO_ORDER);
        }
        if (!user.getId().equals(order.getDoctorId())&&!user.getId().equals(order.getInquiryManId())) {
            throw new CommonException(CodeMsg.ERROR_NO_permission);
        }
        //判断订单是否有剩余时间
        Integer leftVedioTime = order.getLeftVedioTime();
        if (0 >= leftVedioTime) {
            throw new CommonException(CodeMsg.ERROR_NO_LEFT_TIME);
        }
    }


}
